package com.esunrising.esungsc.controller;

import com.esunrising.esungsc.common.utils.HttpUtil;
import com.esunrising.esungsc.config.GSCAPIConfig;
import com.esunrising.esungsc.dto.FIGLZWPZ;
import com.esunrising.esungsc.dto.Result;
import com.esunrising.esungsc.entity.BFCustomItem;
import com.esunrising.esungsc.service.FIGLAccountingDocumentService;
import com.esunrising.esungsc.service.GSCService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/gsc")
@Api(value = "/api/v1/gsc", tags = {"GSC接口处理模块"})
@Slf4j
public class GSCController {

    @Resource
    private GSCService gscService;

    @Resource
    private FIGLAccountingDocumentService figlAccountingDocumentService;


    @PostMapping("/init")
    public Result<Void> init(String dwbh, String kjnd){
        String accOrgId, accOrgCode, ledger, kmtxId, sdate, edate;

        sdate = kjnd+"-01-01 00:00:00";
        edate = kjnd+"-12-31 23:59:59";
        String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        if(now.compareTo(edate) < 0){
            edate = now;  // 当前日期小于截止日期，则取当前日期
        }
        // TODO:根据帐套配置表获取配置信息

        accOrgId = "";
        accOrgCode = "03038";
        ledger = "";
        kmtxId = "";

        // 01 调用凭证列表接口
        figlAccountingDocumentService.voucherList(dwbh, accOrgCode, kjnd, sdate, edate);
        return Result.ok();

        // 02 核算单位

        // 03 科目体系 系统一年获取一次就行
    }


    /**
     * 获取凭证列表数据
     * @return
    */
    @GetMapping("/customItem")
    public Object customItem(){

        String year = "2024";
        String hszzid = "010511";
        String lastChangedOn = "2024-05-09 02:00:48";
        String endDate = "2024-05-09 13:44:00";

        List<BFCustomItem> apiCustomItem = gscService.getApiCustomItem(year, hszzid, lastChangedOn, endDate);
        gscService.syncCustomItemList(apiCustomItem);
        return apiCustomItem;
    }
}
